#!/usr/bin/perl
use strict;
use modules::encode;

package cscycle;

sub cscycle {

    my ($dbh,$remote,$input,$csnum) = @_;
    my $sth;my $auth;

    if ($$input[4] eq "") {
	print $remote "$csnum O $$input[0] :Syntax :CYCLE <#channel>\n";
    }
    else {
	$sth=$dbh->prepare("SELECT auth FROM ninfo WHERE num = '$$input[0]' AND code = '".encode::encode($$input[0])."';");
	$sth->execute;
	if (!($auth = $sth->fetchrow_array)) {
	    print $remote "$csnum O $$input[0] :You have to identify yourselve with nickserv first.\n";
	}
	else {
	    $sth=$dbh->prepare("SELECT level FROM users WHERE channel = '$$input[4]' AND rnick = '$auth' AND level >= 499;");
	    $sth->execute;
	    if (!($sth->fetchrow_array)) {
		print $remote "csnum O $$input[0] :Access denied.\n";
	    }
	    else {
		$sth=$dbh->prepare("SELECT nick FROM ninfo WHERE num = '$$input[0]' AND code = '".encode::encode($$input[0])."';");
		$sth->execute;
		my $num = $csnum; chop $num; chop $num; chop $num;
		my $tmp = $sth->fetchrow_array;
		print $remote "$csnum L $$input[4] :Cycle requested by $tmp\n$csnum J $$input[4]\n";
		print $remote "$csnum O $$input[0] :Done.\n";
		print $remote "$num M $$input[4] +o $csnum\n";
	    }
	}
    }
}
1;
